package net.sxnic.ugr.dept.dao;

import java.util.List;

import net.sxinfo.core.spring25.HibernateDao;
import net.sxnic.ugr.dept.Dept;
import net.sxnic.ugr.user.User;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

@Repository("deptDao")
public class HibernateDeptDao extends HibernateDao<Dept, String> implements
		DeptDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Dept> findByUser(User user) {
		Query query = getCurrSession().createQuery("from Dept as d where ? in elements(d.users)");
		query.setParameter(0, user);
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Dept> getTopList() {
		Query query = getCurrSession().createQuery(" from Dept where parent.id is null order by deptOrder asc ");
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Dept> getChildList(String root) {
		Query query = getCurrSession().createQuery("from Dept " + 
				" where parent.id=? order by  deptOrder asc " );
		query.setParameter(0, root);
		return query.list();
	}	
	
	/**
	 * 根据部门类别获取部门List
	 * @param ctype 部门类别
	 * @return 获取指定类型的部门list
	 */
	@SuppressWarnings("unchecked")
	public List<Dept> getDeptByType(String ctype) {
		String hql = "from Dept where ctype=? order by deptOrder";
		return find(hql, new Object[]{ctype});
	}
 
}
